<template>
  <rx-dialog @handOk="cancel" :showcancel="false" :oktext="$ti18('rxForm.closeBtn','关闭')" btnalign="right" order="top">
      <div slot="toolbar">
        <a-button type="primary" @click="downTemp">{{i18n('list.downTemplates','下载EXCEL模板')}}</a-button>
        <a-button type="primary"
                  :disabled="fileList.length === 0"
                  :loading="uploading" @click="onUpload">{{$ti18('rxList.upload','上传')}}</a-button>
      </div>
    <rx-layout>
      <div slot="center" style="padding: 10px;">
        <a-upload :file-list="fileList" :remove="handleRemove" :before-upload="beforeUpload">
          <a-button> <a-icon type="upload" />{{i18n('list.selectFileBtn','请选择文件')}}</a-button>
        </a-upload>
        <form ref="hiform" action="/api/api-form/form/core/formBoList/downTemp" method="post">
          <input ref="boListKey" type="hidden" name="boListKey" v-model="listKey">
        </form>
      </div>
    </rx-layout>

  </rx-dialog>
</template>

<script>
    import {RxDialog,Util} from "@lowcode/jpaas-common-lib";
    import FormBoListApi from "@/api/form/core/formBoList";
    export default {
      name: "FormBoListImportExcelDialog",
      props: {
        boListKey: {
          type: String,
          default: ""
        },
        layerid: {
          type: String,
          default: ""
        },
        destroy: {
          type: Function
        }
      },
      components: {
        RxDialog
      },
      created() {
        this.listKey=Util.encryptWord(this.boListKey,true);
      },
      data(){
        return {
          listKey:'',
          fileList: [],
          uploading: false
        }
      },
      methods:{
          i18n(name,text,key){
              return this.$ti18(name,text,"formBoList",key);
          },
        cancel(){
          Util.closeWindow(this,"cancel");
        },
        downTemp(){
          this.$refs.hiform.submit();
        },
        onUpload(){
          const { fileList,listKey } = this;
          this.uploading = true;
          const formData = new FormData();
          fileList.forEach(file => {
            formData.append('files[]', file);
          });
          formData.append('boListKey',listKey);
          FormBoListApi.importExcel(formData).then(res=>{
            if(res.success) {
              Util.closeWindow(this,"ok");
            }
            this.uploading = false;
          }).catch(res => {
            this.uploading = false;
          });
        },
        handleRemove(file) {
          const index = this.fileList.indexOf(file);
          const newFileList = this.fileList.slice();
          newFileList.splice(index, 1);
          this.fileList = newFileList;
        },
        beforeUpload(file) {
          this.fileList = [...this.fileList, file];
          return false;
        }
      }
    }
</script>

<style scoped>

</style>